package com.xinggq.user.service.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xinggq.common.CommonResult;
import com.xinggq.common.context.UserContext;
import com.xinggq.common.page.PageCondition;
import com.xinggq.user.api.dto.userrole.UserRoleCreateRequest;
import com.xinggq.user.api.dto.userrole.UserRoleQueryRequest;
import com.xinggq.user.api.dto.userrole.UserRoleVO;
import com.xinggq.user.service.service.UserRoleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户角色关联管理控制器
 *
 * @author system
 * @since 2025-01-10
 */
@RestController
@RequestMapping("/api/user-role")
@Tag(name = "用户角色关联管理", description = "用户角色关联管理相关接口")
@Slf4j
@AllArgsConstructor
@Validated
public class UserRoleController {

  private final UserRoleService userRoleService;

  /**
   * 创建用户角色关联
   */
  @PostMapping
  @Operation(summary = "创建用户角色关联", description = "为用户分配角色")
  public CommonResult<UserRoleVO> createUserRole(
      @Valid @RequestBody UserRoleCreateRequest request) {
    request.setAssignedBy(UserContext.getCurrentUserId());
    UserRoleVO result = userRoleService.createUserRole(request);
    return CommonResult.ok(result);
  }

  /**
   * 删除用户角色关联
   */
  @GetMapping("/del")
  @Operation(summary = "删除用户角色关联", description = "根据关联ID删除用户角色关联")
  public CommonResult<Boolean> deleteUserRole(@RequestParam("userId") Long userId) {
    boolean result = userRoleService.deleteUserRole(userId);
    return CommonResult.ok(result);
  }

  /**
   * 查询用户的角色列表
   */
  @GetMapping("/user")
  @Operation(summary = "查询用户角色列表", description = "根据用户ID查询该用户拥有的所有角色")
  public CommonResult<List<UserRoleVO>> getUserRoles(@RequestParam("userId") Long userId) {
    List<UserRoleVO> result = userRoleService.getUserRoles(userId);
    return CommonResult.ok(result);
  }

  /**
   * 查询角色的用户列表
   */
  @GetMapping("/role")
  @Operation(summary = "查询角色用户列表", description = "根据角色ID查询拥有该角色的所有用户")
  public CommonResult<List<UserRoleVO>> getRoleUsers(@RequestParam("roleId") Long roleId) {
    List<UserRoleVO> result = userRoleService.getRoleUsers(roleId);
    return CommonResult.ok(result);
  }

  /**
   * 批量删除用户的所有角色关联
   */
  @DeleteMapping("/del-user")
  @Operation(summary = "批量删除用户角色关联", description = "删除指定用户的所有角色关联")
  public CommonResult<Boolean> deleteUserRolesByUserId(@RequestParam ("userId") Long userId) {
    boolean result = userRoleService.deleteUserRolesByUserId(userId);
    return CommonResult.ok(result);
  }

  /**
   * 批量删除角色的所有用户关联
   */
  @DeleteMapping("/del-role")
  @Operation(summary = "批量删除角色用户关联", description = "删除指定角色的所有用户关联")
  public CommonResult<Boolean> deleteUserRolesByRoleId(@RequestParam("roleId") Long roleId) {
    boolean result = userRoleService.deleteUserRolesByRoleId(roleId);
    return CommonResult.ok(result);
  }
}